package model;

import java.rmi.RemoteException;

import rmi.server.ServerRemote;
import toolkit.Debug;
import toolkit.Random;

public class Participant
{
	private final Player player;
	
	private final BetContainer betContainer;
	
	private final PlayerGameStats gameStats;
	
	private final Hand hand;
	
	private int position;
	
	private Table table;
	
	private ChosenAction lastChosenAction;
	
	private Combination lastCombination;
	
	private int endPosition = 0;
	
	private int potIndex = -1;
	
	private int seatIndex;
	
	public Participant(Player player)
	{
		this.player = player;
		
		betContainer = new BetContainer();
		betContainer.setChips(2000);
		
		gameStats = new PlayerGameStats();
		
		hand = new Hand();
	}
	
	public PlayerGameStats getGameStats()
	{
		return gameStats;
	}
	
	public Player getPlayer()
	{
		return player;
	}
	
	public void setEndPosition(int endPosition)
	{
		this.endPosition = endPosition;
	}
	
	public int getEndPosition()
	{
		return endPosition;
	}
	
	public int getUserId()
	{
		return player.getUserId();
	}
	
	public int getGameId()
	{
		return table.getGameId();
	}
	
	public BetContainer getBetContainer()
	{
		return betContainer;
	}
	
	public int getStack()
	{
		return betContainer.getChips();
	}
	
	public Hand getHand()
	{
		return hand;
	}

	public void setSeatIndex(int seatIndex)
	{
		this.seatIndex = seatIndex;
	}
	
	public int getSeatIndex()
	{
		return seatIndex;
	}
	
	public void setPosition(int position)
	{
		this.position = position;
	}
	
	public int getPosition()
	{
		return position;
	}
	
	public String getPositionString()
	{
		switch (position)
		{
			case 1:
				
				return "Dealer";
			
			case 2:
				
				return "Small blind";
			
			case 3:
				
				return "Big blind";
			
			default:
				
				if (position >= 4)
				{
					return "UTG+" + (position - 4);
				}
		}
		
		return null;
	}
	
	public void setPotIndex(int potIndex)
	{
		this.potIndex = potIndex;
	}
	
	public int getPotIndex()
	{
		return potIndex;
	}
	
	public ChosenAction chooseAction(int highestStateBet)
	{
		if (!Debug.isTextual())
		{
			ChosenAction chosenAction = null;
			
			chosenAction = ServerRemote.getInstance().chooseAction(this);
			
			if (chosenAction == null)
			{
				chosenAction = new ChosenAction(Action.FOLD);
			}
			
			lastChosenAction = chosenAction;
			
			return chosenAction;
		}
		else
		{
			Action action;
			
			// From 100% there is a 70% chance of Call.
			
			if (Random.chance(30) && (betContainer.getChips() > highestStateBet))
			{
				action = Action.RAISE;
			}
			// From 30% there is a 90% chance (so 27% of 100%) of Raise.
			else if (Random.chance(95))
			{
				action = Action.CALL;
			}
			// Otherwise it will be fold.
			else
			{
				action = Action.FOLD;
			}
			
			ChosenAction chosenAction = new ChosenAction(action);
			
			if (action == Action.RAISE)
			{
				chosenAction.setAmountRaised(Random.random(40, 100));
			}
			
			lastChosenAction = chosenAction;
			
			return chosenAction;
		}
	}
	
	public void showHandCards()
	{
		
	}
	
	public void setTable(Table table)
	{
		this.table = table;
	}
	
	public Table getTable()
	{
		return table;
	}

	public void setLastChosenAction(ChosenAction lastChosenAction)
	{
		this.lastChosenAction = lastChosenAction;
	}
	
	public ChosenAction getLastChosenAction()
	{
		return lastChosenAction;
	}
	
	public Action getLastAction()
	{
		return lastChosenAction.getAction();
	}

	public void setLastCombination(Combination lastCombination)
	{
		this.lastCombination = lastCombination;
	}
	
	public Combination getLastCombination()
	{
		return lastCombination;
	}
	
	public String toString()
	{
		return player.toString();
	}
}
